from django.http import JsonResponse
from django.shortcuts import render
from rest_framework.views import APIView

from apps.user.models import User
from apps.user.serializers import UserSerializer
from utils import ResponseMessage
from utils.password_encode import get_md5


# Create your views here.
class UserApiView(APIView):
    """# 废弃
    def post(self, request):
        request.data["password"] = get_md5(request.data.get("password"))
        user_data_serializer = UserSerializer(data=request.data)
        user_data_serializer.is_valid(raise_exception=True)
        user_data = User.objects.create(**user_data_serializer.data)
        user_ser = UserSerializer(instance=user_data)
        return JsonResponse(user_ser.data)
    """
    def post(self, request):
        user_data_serializer = UserSerializer(data=request.data)
        user_data_serializer.is_valid(raise_exception=True)
        user_data = user_data_serializer.save()

        user_ser = UserSerializer(instance=user_data)
        # return JsonResponse(user_ser.data)
        return ResponseMessage.UserResponse.success(user_ser.data)


